import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

extension NavigatorUtil on Navigator {
  static Future<T?> pushMaterialPage<T extends Object?>(
      BuildContext context, Widget widget) {
    return Navigator.push(context, MaterialPageRoute(builder: (buildContext) {
      return widget;
    }));
  }

  static Future<T?> pushPage<T extends Object?>(
      BuildContext context, Widget widget) {
    return Navigator.push(context, CupertinoPageRoute(builder: (buildContext) {
      return widget;
    }));
  }

  static Future<T?> pushReplacementPage<T extends Object?>(
      BuildContext context, Widget widget) {
    return Navigator.pushReplacement(context,
        CupertinoPageRoute(builder: (buildContext) {
      return widget;
    }));
  }

  static popUntilHome(BuildContext context) {
    Navigator.of(context).popUntil(ModalRoute.withName('/root'));
  }

  static popUntilWelcome(BuildContext context) {
    Navigator.of(context).popUntil(ModalRoute.withName('/'));
  }


  ///底部弹窗动画
  static Future<T?> pushPageWithAnim<T extends Object?>(
      BuildContext context, Widget widget) {
    return Navigator.of(context).push(PageRouteBuilder(
      pageBuilder: (context, animation, secondaryAnimation) => widget,
      transitionsBuilder: (context, animation, secondaryAnimation, child) {
        const begin = Offset(0.0, 1.0);
        const end = Offset.zero;
        const curve = Curves.ease;

        var tween =
            Tween(begin: begin, end: end).chain(CurveTween(curve: curve));

        return SlideTransition(
          position: animation.drive(tween),
          child: child,
        );
      },
    ));
  }
}
